#include "gpsrecorder.h"
#include "ui_gpsrecorder.h"
#include <QDebug>

GpsRecorder::GpsRecorder(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::GpsRecorder)
{
    ui->setupUi(this);
    rConfig = RecordConfig::config();
//    ui->collect_pos->setText(rConfig->getMapFilePath());
    posMark = 0;
    posFlag = 0;
    posNum = 0;
    scanTime.start();
    f = NULL;
//    fp = NULL;
}

void GpsRecorder::setPosMark(int mark)
{
    posMark = mark;
}

void GpsRecorder::setPosFlag(int flag)
{
    posFlag = flag;
}

void GpsRecorder::recordStart()
{
    posNum = 0;
}

GpsRecorder::~GpsRecorder()
{
    delete ui;
}

void GpsRecorder::saveNewPos(double x, double y, QString yaw, QString speed, QString latitude, QString longitude)
{
    if(f == NULL)
        return;

    int flag = posFlag;
    if(posNum == 0)
    {
        scanTime.restart();
    }

    if(posMark != 1)
        flag = 0;
//    if(posMark == 1)
    QString str = QString("%1 %2 %3 %4 %5 %6 %7 %8\n").arg(QString::number(x, 'f', 6)).arg(QString::number(y, 'f', 6)).arg(yaw).arg(speed).arg(latitude).arg(longitude).arg(posMark).arg(flag);
//    else
//        QString str = QString("%1 %2 %3 %4 %5 %6 %7\n").arg(QString::number(x, 'f', 6)).arg(QString::number(y, 'f', 6)).arg(yaw).arg(speed).arg(latitude).arg(longitude).arg(posMark);

    emit newMapPoint(x, y, yaw, speed, latitude, longitude, posMark);
    QByteArray qba = str.toLocal8Bit();
    fputs(qba.data(), f);
//    f->write(str.toLocal8Bit());
    posNum++;
    quint32 timesec = scanTime.elapsed()/1000;
    if(timesec != 0)
    {
        qDebug()<<"posNum:"<<posNum<<"time:"<<timesec<<"HZ:"<<posNum/timesec;
    }
}

void GpsRecorder::saveMap()
{
    qDebug()<<"[saveMap]:file close.";
    fclose(f);
//    f->close();qDebug()<<"[saveMap]:file deleteLater";
//    f->deleteLater();
    qDebug()<<"[saveMap]:file NULL";
    f = NULL;qDebug()<<"[saveMap]:UI clear";
    ui->map_name->clear();
}

void GpsRecorder::showEvent(QShowEvent *)
{
    ui->collect_pos->setText(rConfig->getMapFilePath());
}

void GpsRecorder::on_ok_clicked()
{
    QString fileName = ui->map_name->text();
    if(fileName.isEmpty())
        fileName = QDateTime::currentDateTime().toString("yyyyMMddhhmmss");
    QString filePath = ui->collect_pos->text() + "/" +fileName;
    QByteArray qba = filePath.toLocal8Bit();
//    qba.append((char)0x00);

    if(f != NULL)
    {
        fclose(f);
//        if(f->isOpen())
//            f->close();
//        f->deleteLater();
    }
    qDebug()<<qba;
    f = fopen(qba.data(), "w+");
//    f = new QFile(filePath);
    if(f != NULL)
//    if(f->open(QFile::ReadWrite))
    {
        this->hide();
        emit readyRecorder();
    }
    else
    {
//        fclose(f);
//        f->deleteLater();
        f = NULL;
        ui->msg->setText("地图文件创建失败");
    }
}

void GpsRecorder::on_back_clicked()
{
    ui->msg->clear();
    this->hide();
}

void GpsRecorder::on_browse_clicked()
{
    QString mapUrl = QFileDialog::getExistingDirectory(NULL, "请选择地图文件保存位置", qApp->applicationDirPath());
    if(!mapUrl.isEmpty())
    {
        rConfig->setMapFilePath(mapUrl);
        ui->collect_pos->setText(mapUrl);
    }
}
